<?php
namespace osc\api\controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 14:18
 */

use think\Db;
use think\Request;


class Index extends Base
{
    private $member;
    private $bonus;
    private $info;
    private $b1;
    private $b2;

    public function __construct()
    {
        parent::__construct();
        //用户模型
        $this->member       =   new \osc\api\model\Member();

        //收益模型
        $this->bonus        =   new \osc\api\model\Bonus();

        //合约模型
        $this->info         =   new \osc\api\model\ContractInfo();

        //自由买卖合约模型
        $this->b1           =   new \osc\api\model\ContractB1();

        //目标性合约模型
        $this->b2           =   new \osc\api\model\ContractB2();

    }

    /**
     * 首页接口
     * @return string
     */
    public function index(){

        if($this->request->isPost()){
            $x_uid = $this->request->post('uid');
            if(!$x_uid){
                return json_encode(['check'=>0, 'mage'=>'提交错误!']);
            }

            //解密
            $uid = think_ucenter_decrypt($x_uid,config('PWD_KEY'));
            //用户信息
            $trade = $this->member->where('uid',$uid)->find();

            if(!$trade){
                return json_encode(['check'=>-1, 'mage'=>'用户不存在!']);
            }

            //累计收益
            $inc = $this->bonus->sum_num($trade['username']);

            //合约信息
            $info = $this->info->where('type',1)->find();

            //查询合约
            $b1 = $this->b1->where(['username'=>$trade['username'],'state'=>['<>',3]])->order('id desc')->count();
            $info['state'] = $b1;
            $data = [
                'user' => [
                    'uid'           => $x_uid,
                    'ticket'        => $trade['ticket'],
                    'predeposit'    => $trade['predeposit'],
                    'inc'           => $inc
                ],
                'info'  => $info
            ];

            return json_encode(['check'=>1, 'mage'=>'OK', 'data'=>$data]);
        }else{
            return json_encode(['check'=>0, 'mage'=>'提交错误!']);
        }
    }

}